package com.jule.teenpatti.mail.model;

import com.jule.core.jedis.StoredObj;
import com.jule.teenpatti.mail.dao.bean.MailArgsModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
@Slf4j
public class MailArgsMap extends StoredObj {
    /**
     * mailConfigId,Map
     */
    private Map<Integer, Map<Integer, MailArgsModel>> allMailArgs = new ConcurrentHashMap<>();

    /**
     * 添加邮件配置信息
     *
     * @param list
     */
    public void addItem(List<MailArgsModel> list) {
        Iterator<MailArgsModel> iter = list.iterator();
        while (iter.hasNext()) {
            MailArgsModel mailConfig = iter.next();
            log.debug("MailArgsModel:" + mailConfig.toString());
            Map<Integer, MailArgsModel> all = allMailArgs.get(mailConfig.getMail_id());

            if (all == null) {
                all = new ConcurrentHashMap<>();
                allMailArgs.put(mailConfig.getMail_id(), all);
            }
            all.put(mailConfig.getId(), mailConfig);
        }
    }

    public List<MailArgsModel> getByMailConfigId(int mailConfigId) {
        List<MailArgsModel> list = new ArrayList<>();
        Map<Integer, MailArgsModel> all = allMailArgs.get(mailConfigId);
        if (all != null) {
            Iterator<MailArgsModel> item = all.values().iterator();
            while (item.hasNext()) {
                MailArgsModel argsModel = item.next();
                log.debug("MailArgsModel:" + argsModel.toString());
                list.add(argsModel);
            }
        }
        return list;
    }

    public void del(int mailConfigId) {
        allMailArgs.remove(mailConfigId);
    }
}
